package com.StarStudio.Service.Impl;

import cn.hutool.core.util.IdUtil;
import com.StarStudio.DAO.CouponsDAO;
import com.StarStudio.DAO.ExchangeDAO;
import com.StarStudio.DAO.GoodsDAO;
import com.StarStudio.DAO.UserInfoDAO;
import com.StarStudio.Service.RemoteSysUserService;
import com.ruoyi.common.core.Entity.*;
import com.StarStudio.Service.ExchangeService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private ExchangeDAO exchangeDAO;

    @Autowired
    CouponsDAO couponsDAO;

    @Autowired
    GoodsDAO goodsDAO;

    @Autowired
    UserInfoDAO userInfoDAO;

    @Autowired
    RemoteSysUserService remoteSysUserService;

    @Override
    public List<Exchange> findExchangeList(Exchange exchange) {
        List<Exchange> exchangeList = exchangeDAO.findExchangeList(exchange);
        if (exchangeList != null && exchangeList.size() > 0){
            for (Exchange exchange1 : exchangeList) {
                Map<String,Object> param = new HashMap<>();
                if (exchange1.getGoods_item_id() != null && exchange1.getGoods_item_id() > 0){
                    Goods goods = goodsDAO.findByGoodsItemId(exchange1.getGoods_item_id());
                    param.put("goodsName",goods.getGoods_name());
                }else {
                    Coupons coupons = couponsDAO.findByCoupons_item_id(exchange1.getCoupons_item_id());
                    param.put("couponsName",coupons.getCoupon_name());
                }
                UserInfo userInfo = userInfoDAO.findByUser_id(exchange1.getExchange_user_id());
                param.put("userName",userInfo.getReal_name());
                SysUser sysUserById = remoteSysUserService.getSysUserById(exchange1.getStore_id());
                param.put("storeName",sysUserById.getNickName());
                exchange1.setParams(param);
            }
        }
        return exchangeList;
    }

    @Override
    public Exchange findByExchange_id(long id) {
        return exchangeDAO.findByExchange_id(id);
    }

    @Override
    public List<Exchange> findByExchange_user_id(long id) {
        return exchangeDAO.findByExchange_user_id(id);
    }

    @Override
    public void deleteExchangeById(long id) {
        exchangeDAO.deleteExchangeById(id);
    }

    @Override
    public void addExchange(Exchange exchange) {
        exchange.setExchange_id(IdUtil.getSnowflakeNextId()); //使用雪花ID
        exchangeDAO.addExchange(exchange);
    }

    @Override
    public void updateExchange(Exchange exchange) {
        exchangeDAO.updateExchange(exchange);
    }

    @Override
    public long findAllRecordNumber() {
        return exchangeDAO.findAllRecordNumber();
    }

    @Override
    public void updateExchangeStatus(long coupons_item_id, String status,String exchange_store) {
        exchangeDAO.updateExchangeStatus(coupons_item_id, status,exchange_store,new Timestamp(new Date().getTime()));
    }

    @Override
    public Exchange findByCoupons_item_id(long coupons_item_id) {
        return exchangeDAO.findByCoupons_item_id(coupons_item_id);
    }

    @Override
    public SysUser findSysUser(Login login) {
        return exchangeDAO.findSusUserByUsernameAndPassword(login);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateExchangeStatusAndLink(long couponsItemId, String status, String exchangeStore, String link) {
        exchangeDAO.updateExchangeStatusAndLink(couponsItemId,status,exchangeStore,link);
    }


    /**
     * 定时检查优惠券是否过期  若过期则更新状态
     * 每隔 1小时检查一次
     */
    @Scheduled(cron = "0 0 */${time.interval} * * ?")
    public void AutoCheckExpiredStatus(){
        List<Long> unExpiredCouponsItemIds = exchangeDAO.findUnExpiredCouponsItemIds(MessageConstant.EXCHANGE_STATUS_EXCHANGED_BUT_NOT_USED);
        if (unExpiredCouponsItemIds.size() > 0){
            for (Long unExpiredCouponsItemId : unExpiredCouponsItemIds) {
                Coupons byCoupons_item_id = couponsDAO.findByCoupons_item_id(unExpiredCouponsItemId);
                if (new Timestamp(new Date().getTime()).after(byCoupons_item_id.getDate_use_end())){
                    //已过期   更新 Exchange状态
                    exchangeDAO.updateExchangeStatusToExpired(unExpiredCouponsItemId,MessageConstant.EXCHANGE_STATUS_EXPIRED);
                }
            }
        }
    }

}
